$(function(){
    var orign = document.getElementById("orign");//这个是第一个界面
    var obj = document.getElementById("inventoryAdd");//这个是第二个界面
    var objEdit = document.getElementById("inventoryEdit");//这个是第三个界面
    var confirmButton = document.getElementsByName("inventoryNew");//这个是新增按钮
    var EditButton = document.querySelectorAll('.inventoryEditBtn');//这个是编辑按钮的集合

    obj.style.display="none";
    objEdit.style.display="none";
    var names='';
    var values='';
    console.log("hello");
    $('#inventoryNew').on('click',function(){
        console.log("he");
        orign.style.display="none";//隐藏
        obj.style.display="";//显示
    });
    EditButton.forEach(function(element){
        //将每一个Edit进行绑定
        element.onclick=function(){
            console.log("Edit successfully");
            orign.style.display="none";//隐藏
            objEdit.style.display="";//显示
            //获取主键结点
            var name = this.getAttribute("name");
            var editItemid=document.getElementsByName("editItemid")
            //获取各个属性结点
            var editQty = document.getElementsByName("editQty");
            console.log(name);
            //接下来要写ajax了
            $.ajax({
                type     :'GET',
                url      :'http://localhost:8088/inventoryEditAjax?itemid='+name,
                success  :function (data){
                    //取到键值对了
                    console.log(data.qty);
                    console.log(data.itemid);
                    editItemid[0].value = data.itemid;
                    editQty[0].value=data.qty;

                },
                error    :function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }
    });

    $('#keyword').on('keyup',function (){
        const keyword = $(this).val();
        if(keyword!==null&&keyword!==''&&keyword.length!==0){
            $.ajax({
                type   :'GET',
                url    :'http://localhost:8088/inventorySearchAjax?keyword='+keyword,
                success:function (list){
                    console.log(keyword);
                    let productListHTML = '';
                    for(let i=0; i<list.length; i++){
                        productListHTML+='<li class=\"productAutoItem\" data-itemid="';
                        productListHTML+=list[i].itemid;
                        productListHTML+='">';
                        productListHTML+=list[i].qty;
                        productListHTML+='</li>';
                    }
                    $('#productAutoList').html(productListHTML);
                    $('#productAutoComplete').show();
                },
                error  :function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }else{
            $('#productAutoComplete').hide();
        }

    });

    $(document).on('click','.productAutoItem',function (){
        const itemId = $(this).data('itemid');
        console.log(itemId);
        $('#productAutoComplete').hide();
        $('#keyword').val('');
        window.location.href='http://localhost:8088/inventorySearch?keyword='+itemid ;
    });
});